<!doctype  html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>下载中心</title>
<style type="text/css">
       body{
       	   background: url("image/6_bg.png");
       	   background-size: 100% 100%;
       	   background-repeat: no-repeat;
       	   min-height:450px;
       	   width: 100%;
       }
       form{
       	margin: 0 auto;
       	margin-top:120px;
       	width: 700px;
       	position: relative;
       }
       select{
       	width: 150px;
        height: 20px;
       	margin: 0 auto;
       	position: absolute;
       	left: 275px;
       }
       input{
       	   margin-top: 25px;
       	   width: 200px;

       }
       span{
       	   display: inline-block;
       	   width:50px;
       	   margin-left: 50px;
       }
       .center{
       	   margin-top: 10px;
       	   margin-left:50px;
       }
       #button{
       	   margin-top:20px;
       	   height:25px;
       	   width: 80px;
       	   margin-left:15px;

       }
       .leftbutton{
       	   margin-left: 250px;
       }
       #file{
          display: none;
       }
       #upload{
          width: 100px;
       }
       #sourceUrl{
        width:100px;
       }

</style>
</head>
<body>
	<form>
            <select>
		           <option>下载中心</option>
		 	</select><br>
		     <span>编辑：</span><input type="text" name="editor" id="editor">
		     <span>标题：</span><input type="text" name="title" id="title"><br>
		     <span>来源：</span><input type="text" name="source" id="source">
		     <span class="center">上传：</span><input type="file" name="file" id="file"><input type="button" id="upload" value="上传"><input type="text" id="sourceUrl"><br>
		     <a class="leftbutton" href="content_mag.html">返回</a>
		     <input type="button" id="button" value="提交">
	</form>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="../public/js/serverIp.js"></script>
<script type="text/javascript">

          


        $(function(){

            var array=window.location.href.split("?");
            var substr=array[1].split("&");
            alert(window.location.href)
            var datastr="type=ID&bool=false&columnId="+substr[1]+"&modelId="+substr[0]+"&id="+substr[3];
            if(substr[2]=="edit"){
                $.post(serverIp+"/Scauaie.com/artical/showAll",datastr,function(data){
                  
                     $("#editor").val(data[0].editor);
                     $("#title").val(data[0].title);
                     $("#source").val(data[0].source); 
                     $("#sourceUrl").val(data[0].resourceUrl);
                },"json");

            }


          $("#upload").click(function(){

                $("#file").trigger("click");
                $("#file").change(function(){
                   // alert($("#file")[0].files)   获取文件列表
                   var files=$(this).prop("files");
                   var formdata = new FormData();
                   formdata.append("file",files[0]);
                   
                  $.ajax({
                        type: "post",
                        url: serverIp+"/Scauaie.com/artical/file/upload",
                        data:formdata,
                        dataType:"json",
                        cache: false,  
                        processData: false,  
                        contentType: false,
                        success:function(data){
                           $("#sourceUrl").val(data.result);
                        }
                    })
               })
         })


         

         function submitTo(url,id){
            var arr=window.location.href.split("?");
                var sub=arr[1].split("&");
                var time="2016/4/14";  
                var editor=$("#editor").val();
                var title=$("#title").val();
                var source=$("#source").val();
                var resourceUrl=$("#sourceUrl").val();
                var data={
                    "type":"COLUMN",
                    "bool":false,
                    "modelId":sub[0],
                    "columnId":sub[1],
                    "editor":editor,
                    "title":title,
                    "source":source,
                    "resourceUrl":resourceUrl,
                    "status":true,
                    "likeTimes":10,
                    "id":id,
                    "download_id":id
                };

               if(!editor||!title||!source||!resourceUrl){
                      alert("请先填写完整");
               }else{
                 alert(JSON.stringify(data))
                  $.post(url,{"data":JSON.stringify(data)},function(data){
                        var dataobj=JSON.parse(data);
                        if(dataobj.bool=="true"){
                              if(confirm("提交成功，留在本页面？")){
                                   var href=window.location.href;
                                   window.location.href=href;
                              }else{
                                 window.location.href="content_mag.html";
                              }
                            }else{
                                alert("提交失败，请重新尝试");
                            }
                   },"html")
               }
         }

         $("#button").click(function(){              
                if(substr[2]=="edit"){
                    submitTo(serverIp+"/Scauaie.com/artical/edit",substr[3]);
               }else{
                    submitTo(serverIp+"/Scauaie.com/artical/add",null);
               }  

          })

        })
</script>
</body>
</html>	